package com.ruoyi.web.zangchat.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.web.zangchat.domain.dto.ChatTrendsDTO;
import com.ruoyi.web.zangchat.utils.MongoDBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.zangchat.mapper.ChatTrendsMapper;
import com.ruoyi.web.zangchat.domain.ChatTrends;
import com.ruoyi.web.zangchat.service.IChatTrendsService;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 广场动态 服务层实现
 *
 * @author ruoyi
 * @date 2020-07-22
 */
@Service
public class ChatTrendsServiceImpl extends ServiceImpl<ChatTrendsMapper, ChatTrends> implements IChatTrendsService
{
    @Autowired
    private MongoDBUtil mongoDBUtil;
    @Override
    public List<Map<String, Object>> chatTrendsList(Map<String, Object> map) {
        return baseMapper.chatTrendsList(map);

    }

    @Override
    public Map<String, Object> getMapById(Long trendsId) {
        Map<String, Object> resultMap=new HashMap<>();
        ChatTrends chatTrends = baseMapper.selectById(trendsId);
//        String gallery = chatTrends.getGallery();
//        if (StringUtils.isNotEmpty(gallery)){
//            String[] galleryList = gallery.split(",");
//            resultMap.put("galleryList",galleryList);
//        }else {
//            resultMap.put("galleryList","");
//        }
        resultMap.put("trendsId",trendsId);
        resultMap.put("userId",chatTrends.getUserId());
        resultMap.put("trendLimit",chatTrends.getTrendLimit());
        resultMap.put("trendsContenttai",chatTrends.getTrendsContenttai());
        resultMap.put("dataFlag",chatTrends.getDataFlag());
        resultMap.put("createTime",chatTrends.getCreateTime());
        resultMap.put("trendsType",chatTrends.getTrendsType());
        resultMap.put("relayId",chatTrends.getRelayId());
        resultMap.put("gallery",chatTrends.getGallery());
        return resultMap;
    }

    @Override
    public ChatTrendsDTO getDTOById(Long trendsId) {
        ChatTrendsDTO chatTrendsDTO=new ChatTrendsDTO();
        ChatTrends chatTrends = baseMapper.selectById(trendsId);
        BeanUtils.copyProperties(chatTrends,chatTrendsDTO);
            String gallery = chatTrends.getGallery();
        if (StringUtils.isNotEmpty(gallery)) {
            String[] galleryList = gallery.split(",");
            chatTrendsDTO.setGalleryList(galleryList);
        }
        return chatTrendsDTO;
    }

    @Override
    public Boolean deleteByIds(String ids) {
        List<String> stringList = Arrays.asList(Convert.toStrArray(ids));
        boolean b = this.removeByIds(stringList);
        for (String id : stringList) {
            mongoDBUtil.deleteById(id);
        }
       return b;
    }
}